package com.gaoboy.springbootgoods.controller;

import com.gaoboy.springbootgoods.entity.Goods;
import com.gaoboy.springbootgoods.service.GoodsService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;

@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/goods/queryByDynamicFields")
    public String queryByDynamicFields(Integer pageNum,
                                       String name,
                                       String startdate,
                                       String enddate,
                                       HttpServletRequest request){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        try {
            PageInfo<Goods> pageInfo = goodsService.queryBynamicFields(pageNum==null?1:pageNum,name,
                    (startdate == null|| "".equals(startdate)?null:dateFormat.parse(startdate)),
                    (enddate == null || "".equals(enddate)?null:dateFormat.parse(enddate))
            );
            request.setAttribute("pageInfo",pageInfo);
            request.setAttribute("startdate",startdate);
            request.setAttribute("enddate",enddate);
            request.setAttribute("name",name);

            return "goodslist";
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
